// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Ending Pose Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 48 : TYPE_ENDINGPOSE

// Function declarations
reserve function EndingPose_Function107

// Static Values

// Tables

function EndingPose_Function107
	if object.value1 > 0
		object.value1 -= 16
	end if
end function


event ObjectMain
	switch object.state
	case 0
		object.state++
		if stage.playerListPos == 1
			object.value2 = 36
		end if
	case 1
		object.value0++
		if stage.playerListPos == 1
			object.value2 = object.value0
			object.value2 >>= 3
			object.value2 %= 5
			object.value2 += 36
		end if
		if object.value0 == 240
			currentPlayer = 1
			while currentPlayer < playerCount
				ResetObjectEntity(currentPlayer, TypeName[Ending Pose], 0, object[currentPlayer].xpos, object[currentPlayer].ypos)
				object[currentPlayer].drawOrder = 6
				object[currentPlayer].state = 14
				object[currentPlayer].frame = 43
				object[currentPlayer].direction = FLIP_X
				currentPlayer++
			loop
			object.value0 = 0
			switch stage.playerListPos
			case 0
			case 3
				object.frame = 1
				break
			case 1
				object.frame = 14
				break
			case 2
				object.frame = 25
				break
			end switch
			object.state++
			object.value2 = 0
		else
			currentPlayer = 1
			while currentPlayer < playerCount
				if object[currentPlayer].speed == 0
					object[currentPlayer].direction = FLIP_X
				end if
				currentPlayer++
			loop
		end if
		break
	case 2
		CallFunction(EndingPose_Function107)
		object.value0++
		if object.value0 == 12
			object.value0 = 0
			switch stage.playerListPos
			case 0
			case 3
				object.frame = 2
				object.ixpos = screen.cameraX
				object.iypos = screen.cameraY
				break
			case 1
			case 2
				break
			end switch
			object.state++
		end if
		break
	case 3
		CallFunction(EndingPose_Function107)
		object.value0++
		if object.value0 == 4
			object.value0 = -0x618
			switch stage.playerListPos
			case 0
			case 3
				object.frame = 3
				break
			case 1
				object.frame = 15
				object.ixpos = screen.cameraX
				object.iypos = screen.cameraY
				break
			case 2
				object.frame = 26
				object.ixpos = screen.cameraX
				object.iypos = screen.cameraY
				break
			end switch
			object.state++
		end if
		break
	case 4
		if object.value0 < 320
			object.value0 += 12
		else
			stage.activeList = PRESENTATION_STAGE
			stage.listPos = 2
			lampPostID = 0
			recMilliSeconds = 0
			recSeconds = 0
			recMinutes = 0
			LoadStage()
		end if
		if object.value0 > 0
			temp0 = object.value0
		else
			temp0 = 0
		end if
		SetScreenFade(0, 0, 0, temp0)
		break
	case 5
		object.value0++
		if object.value0 == 80
			object.value0 = 0
			object.state++
		else
			switch stage.playerListPos
			case 0
			case 3
				object.frame = 4
				break
			case 1
				object.frame = 16
				break
			case 2
				object.frame = 27
				break
			end switch
			currentPlayer = 1
			while currentPlayer < playerCount
				if object[currentPlayer].speed == 0
					object[currentPlayer].direction = FLIP_X
				end if
				currentPlayer++
			loop
		end if
		break
	case 6
		object.frame = object.value0
		object.frame >>= 2
		object.frame &= 1
		switch stage.playerListPos
		case 0
		case 3
			object.frame += 5
			break
		case 1
			object.frame += 17
			break
		case 2
			object.frame += 27
			break
		end switch
		object.value0++
		if object.value0 == 80
			object.value0 = 0
			switch stage.playerListPos
			case 0
			case 3
				object.frame = 7
				break
			case 1
				object.frame = 19
				break
			case 2
				object.frame = 29
				break
			end switch
			currentPlayer = 1
			while currentPlayer < playerCount
				object[currentPlayer].up = 1
				object[currentPlayer].controlMode = -1
				currentPlayer++
			loop
			CreateTempObject(TypeName[Emeralds], 0, object.xpos, object.ypos)
			object[tempObjectPos].drawOrder = 6
			object.state++
		else
			currentPlayer = 1
			while currentPlayer < playerCount
				if object[currentPlayer].speed == 0
					object[currentPlayer].direction = FLIP_X
				end if
				currentPlayer++
			loop
		end if
		break
	case 7
		break
	case 8
		if object.value0 < 384
			object.value0 += 6
		else
			object.value0 = 256
			SetTileLayerEntry(184, 0, 2, 2)
			SetTileLayerEntry(185, 0, 3, 2)
			SetTileLayerEntry(186, 0, 2, 3)
			SetTileLayerEntry(187, 0, 3, 3)
			SetTileLayerEntry(188, 0, 4, 2)
			SetTileLayerEntry(189, 0, 5, 2)
			SetTileLayerEntry(190, 0, 4, 3)
			SetTileLayerEntry(191, 0, 5, 3)
			object.state++
		end if
		SetScreenFade(208, 255, 224, object.value0)
		break
	case 9
		if object.value0 > 0
			object.value0 -= 16
			SetScreenFade(208, 255, 224, object.value0)
		else
			switch stage.playerListPos
			case 0
			case 3
				object.frame = 8
				break
			case 1
				object.frame = 20
				break
			case 2
				object.frame = 30
				break
			end switch
			object.state++
			currentPlayer = 1
			while currentPlayer < playerCount
				ResetObjectEntity(currentPlayer, TypeName[Ending Pose], 0, object[currentPlayer].xpos, object[currentPlayer].ypos)
				object[currentPlayer].drawOrder = 6
				object[currentPlayer].state = 12
				object[currentPlayer].frame = 43
				object[currentPlayer].direction = FLIP_X
				currentPlayer++
			loop
		end if
		break
	case 10
		switch stage.playerListPos
		case 0
		case 3
			object.frame = object.value0
			object.frame /= 6
			object.frame &= 1
			object.frame += 8
			break
		case 1
			object.frame = object.value0
			object.frame /= 10
			object.frame &= 3
			object.frame += 20
			if object.value0 >= 40
				object.frame = 23
			end if
			break
		case 2
			object.frame = object.value0
			object.frame /= 10
			object.frame &= 3
			object.frame += 30
			if object.value0 >= 40
				object.frame = 34
			end if
			break
		end switch
		object.value0++
		if object.value0 == 42
			object.value0 = 0
			object.state++
		end if
		break
	case 11
		object.value0++
		if object.value0 == 60
			object.value0 = 0
			switch stage.playerListPos
			case 0
			case 3
				object.frame = 1
				break
			case 1
				object.frame = 14
				break
			case 2
				object.frame = 25
				break
			end switch
			object.state = 2
		end if
		break
	case 12
		object.frame = object.value0
		object.frame /= 10
		object.frame &= 3
		object.frame += 20
		if object.value0 >= 40
			object.frame = 23
		end if
		object.value0++
		if object.value0 == 42
			object.value0 = 0
			object.state++
		end if
		break
	case 13
		object.value0++
		if object.value0 == 60
			object.value0 = 0
			object.frame = 43
			object.state++
		end if
		break
	case 14
		object.value0++
		if object.value0 == 8
			object.frame++
			object.value0 = 0
			object.state++
		end if
		break
	end switch
end event


event ObjectDraw
	if object.value2 != 0
		DrawSprite(object.value2)
	end if
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	if object.entityPos == 0
		if object.state > 0
			temp0 = screen.xcenter
			temp0 -= 96
			temp0 -= object.value1
			if stage.player2Enabled == 1
				if options.region == 1
					temp1 = 42
				else
					temp1 = 41
				end if
			else
				temp1 = 10
				if stage.playerListPos < 3
					temp1 += stage.playerListPos
				end if
			end if
			DrawSpriteScreenXY(temp1, temp0, 80)
		end if
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Ending/Objects.gif")
	SpriteFrame(-14, -20, 32, 39, 353, 146)
	SpriteFrame(-16, -16, 32, 35, 3, 220)
	SpriteFrame(-18, -27, 44, 71, 36, 184)
	SpriteFrame(-56, -68, 174, 134, 81, 121)
	SpriteFrame(-10, -21, 27, 39, 386, 146)
	SpriteFrame(-10, -21, 24, 40, 3, 16)
	SpriteFrame(-14, -21, 28, 40, 3, 57)
	SpriteFrame(-16, -20, 29, 39, 3, 98)
	SpriteFrame(-16, -21, 32, 40, 3, 138)
	SpriteFrame(-16, -21, 32, 40, 3, 179)
	SpriteFrame(-48, 0, 96, 31, 256, 141)
	SpriteFrame(-48, -17, 96, 48, 256, 182)
	SpriteFrame(-48, -17, 96, 48, 256, 124)
	SpriteFrame(-10, -13, 24, 31, 288, 16)
	SpriteFrame(-19, -12, 33, 31, 254, 16)
	SpriteFrame(-55, -48, 111, 96, 400, 1)
	SpriteFrame(-12, -16, 27, 34, 313, 16)
	SpriteFrame(-12, -16, 27, 34, 59, 16)
	SpriteFrame(-15, -16, 30, 34, 87, 16)
	SpriteFrame(-15, -14, 31, 32, 118, 16)
	SpriteFrame(-12, -16, 25, 34, 150, 16)
	SpriteFrame(-12, -16, 25, 34, 176, 16)
	SpriteFrame(-12, -16, 25, 34, 202, 16)
	SpriteFrame(-12, -16, 25, 34, 228, 16)
	SpriteFrame(-14, -23, 27, 41, 325, 57)
	SpriteFrame(-16, -21, 28, 39, 296, 57)
	SpriteFrame(-32, -52, 64, 104, 448, 98)
	SpriteFrame(-12, -23, 24, 41, 32, 57)
	SpriteFrame(-16, -23, 28, 41, 57, 57)
	SpriteFrame(-18, -22, 45, 40, 86, 57)
	SpriteFrame(-16, -23, 33, 41, 132, 57)
	SpriteFrame(-16, -23, 33, 41, 166, 57)
	SpriteFrame(-16, -23, 33, 41, 200, 57)
	SpriteFrame(-16, -23, 33, 41, 200, 57)
	SpriteFrame(-16, -23, 27, 41, 268, 57)
	SpriteFrame(-16, -23, 33, 41, 234, 57)
	SpriteFrame(-21, -6, 16, 24, 256, 99)
	SpriteFrame(-25, -6, 20, 24, 273, 99)
	SpriteFrame(-25, -6, 20, 24, 294, 99)
	SpriteFrame(-25, -6, 20, 24, 315, 99)
	SpriteFrame(-25, -6, 20, 24, 336, 99)
	SpriteFrame(-48, 0, 96, 40, 256, 141)
	SpriteFrame(-48, 0, 96, 40, 256, 199)
	SpriteFrame(-10, -13, 22, 31, 58, 99)
	SpriteFrame(-20, -17, 36, 35, 44, 131)
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
